<?php

namespace App\Admin\Controllers;

use App\Admin\Repositories\Badge;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Show;
use Dcat\Admin\Http\Controllers\AdminController;
use Dcat\Admin\Widgets\Card;

class BadgeController extends AdminController
{
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(new Badge(), function (Grid $grid) {
            $grid->column('id')->sortable();
            $grid->column('title');
            $grid->column('img')->image(null, 50, 50);
            $grid->column('intro');
            $grid->column('explain')
                ->if(function ($column) {
                    return $column->getValue();
                })
                ->then(function (Grid\Column $column) {
                    $column->display('查看')
                        ->modal(function ($modal) {
                            $modal->title($this->title);
                            return "<div style='overflow: auto'>$this->explain</div>";
                        });
                })
                ->else(function (Grid\Column $column) {
                    $column->display('');
                });
            $grid->column('created_at');
            $grid->column('updated_at')->sortable();

            $grid->filter(function (Grid\Filter $filter) {
                $filter->equal('id');

            });
        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, new Badge(), function (Show $show) {
            $show->field('id');
            $show->field('title');
            $show->field('img')->image(null, 50, 50);
            $show->field('intro');
            $show->field('explain');
            $show->field('created_at');
            $show->field('updated_at');
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new Badge(), function (Form $form) {
            $form->text('title');
            $form->image('img')->uniqueName()->saveFullUrl()->autoUpload();
            $form->textarea('intro')->rows(3);
            $form->editor('explain');
        });
    }
}
